package com.example.myapplication

import android.os.Bundle
import android.os.Looper
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.amap.api.location.AMapLocationClient
import com.amap.api.maps.AMap
import com.amap.api.maps.AMap.OnInfoWindowClickListener
import com.amap.api.maps.MapView
import com.amap.api.maps.offlinemap.OfflineMapManager
import com.amap.api.services.busline.BusLineQuery
import com.amap.api.services.busline.BusStationQuery
import com.amap.api.services.busline.BusStationResult
import com.amap.api.services.busline.BusStationSearch
import com.amap.api.services.busline.BusStationSearch.OnBusStationSearchListener
import com.example.myapplication.coreutil.CoreUtil
import com.hjq.toast.Toaster
import okhttp3.internal.ignoreIoExceptions

class MainActivity : AppCompatActivity() {
    private val mMapView: MapView by lazy {
        findViewById<MapView?>(R.id.map).apply {
            Toaster.delayedShow("初始化mapview成功", 2000)
        }
    }
    private var mAmap: AMap? = null


    // good style: 应该永远设置为nullable, 并且初始化为null
    private var mAMapLocationClient: AMapLocationClient? = null
    private lateinit var offlineMapManager: OfflineMapManager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }


        // 无关用例的先放一边
        run {
            CoreUtil.initToaster(this.application)
            CoreUtil.initPermission(this)
            CoreUtil.initGaodeAllPrivacyFirst(this, true, true, true)
        }

        if (mAmap == null) {
            mAmap = mMapView.map
        }
        mMapView.onCreate(savedInstanceState)

//        mAMapLocationClient = AMapLocationClient(this)
        // 用例: 展示我的位置
//        CoreUtil.showMyLocationByAMap(mAmap!!)
        // 用例: 添加一个marker
        CoreUtil.addMarkerOnAMap(mAmap!!)

//        run {
//
//            val lis = OnInfoWindowClickListener {
//                Toast.makeText(
//                    this@MainActivity,
//                    "点击了InfoWindow",
//                    Toast.LENGTH_SHORT
//                ).show()
//            }
//            mAmap!!.setOnInfoWindowClickListener(lis)
//        }
        // 用例: 画线
        CoreUtil.drawLineOnAMap(mAmap!!)
        mAmap!!.mapType = AMap.MAP_TYPE_NORMAL

        CoreUtil.initLocationClient(this)
        // 用例: 随机画点
//        CoreUtil.startRandomLocToPathCollection(mAmap!!)
        // 用例: 画一个圆
        CoreUtil.drawACirleOnAMap(mAmap!!)

//        ignoreIoExceptions {
//            val busStationQuery = BusStationQuery("机场海关", "深圳市")
//            Toaster.show(busStationQuery.city)
//
//
//            val busStationSearch = BusStationSearch(this, busStationQuery)
//            busStationSearch.setOnBusStationSearchListener(object : OnBusStationSearchListener {
//                override fun onBusStationSearched(p0: BusStationResult?, p1: Int) {
//                    if (p0 != null) {
//                        Toaster.delayedShow(p0.busStations, 3000)
//                    }
//                }
//            })
//        }
//
////        CoreUtil.queryAWeather()
//        CoreUtil.getAWetherData(this) { weatherResponse ->
//            Looper.prepare()
//            Toast.makeText(this, "${weatherResponse}", Toast.LENGTH_SHORT).show()
//        }
    }


    override fun onDestroy() {
        super.onDestroy()
        mMapView.onDestroy()
    }

    override fun onResume() {
        super.onResume()
        mMapView.onResume()
    }

    override fun onPause() {
        super.onPause()
        mMapView.onPause()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        mMapView.onSaveInstanceState(outState)
    }
}